package com.cos.hospital.model.retrofit;

import com.cos.hospital.code.Code;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class HostWrapper {
    private static volatile HostWrapper sInstance;
    private Retrofit mRetrofit;

    private HostWrapper() {
        OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
        clientBuilder.connectTimeout(60, TimeUnit.SECONDS);
        clientBuilder.readTimeout(60, TimeUnit.SECONDS);
        clientBuilder.writeTimeout(60, TimeUnit.SECONDS);
        clientBuilder.addInterceptor(new HeaderInterceptor());
        mRetrofit = new Retrofit.Builder()
            .baseUrl(Code.Link.HOST_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .client(clientBuilder.build())
            .build();
    }

    /**
     * 懒汉式
     */
    public static HostWrapper with() {
        if (sInstance == null) {
            synchronized (HostWrapper.class) {
                if (sInstance == null) {
                    sInstance = new HostWrapper();
                }
            }
        }
        return sInstance;
    }

    public <T> T create(Class<T> service) {
        return mRetrofit.create(service);
    }

}